// pages/UserStatistics/AddPersonnel/AddPersonnel.js
const controllers = require('../../controllers/controllerIndex.js').controller;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value:{
      head:"",
      position:"",
      context:"",
      nick:"",
      mobile:"",
      shopId:"",
      power:""
    },
    selectIndex:0,
    isEdit:false,
    levelList:[
      {
        name:"1星",
        id:1
      },
      {
        name:"2星",
        id:2
      },
      {
        name:"3星",
        id:3
      },
      {
        name:"4星",
        id:4
      },
      {
        name:"5星",
        id:5
      }
    ],
    QXList:[
      {
        name:"用户权限",
        tips:"(包含普通用户、普通会员、VIP会员、股东)",
        selected:false,
        power:"USER"
      },
      {
        name:"订单权限",
        tips:"(包含线上、线下开单、预约管理)",
        selected:false,
        power:"ORDER"
      },
      {
        name:"核销权限",
        tips:"(包含优惠券核销、商品核销、套餐卡核销)",
        selected:false,
        power:"WRITE_OFF"
      },
      {
        name:"轮播图权限",
        tips:"",
        selected:false,
        power:"ROTATION"
      },
      {
        name:"商品权限",
        tips:"(包含普通商圈、云商品)",
        selected:false,
        power:"GOODS"
      },
      {
        name:"员工权限",
        tips:"(注意:拥有该权限店员可以操作权限管理)",
        selected:false,
        power:"STAFF"
      },
      {
        name:"拼团权限",
        tips:"",
        selected:false,
        power:"PIN"
      },
      {
        name:"优惠券权限",
        tips:"",
        selected:false,
        power:"COUPON"
      },
      {
        name:"会员卡权限",
        tips:"(包含套餐卡、充值卡、引流卡、线下开卡)",
        selected:false,
        power:"VIP"
      },
      {
        name:"红包权限",
        tips:"",
        selected:false,
        power:"RED"
      },
      {
        name:"商圈权限",
        tips:"",
        selected:false,
        power:"GROUP"
      },
      {
        name:"评论权限",
        tips:"",
        selected:false,
        power:"COMMENT"
      },
      {
        name:"查看营收",
        tips:"",
        selected:false,
        power:"REVENUE"
      },
      {
        name:"记账",
        tips:"",
        selected:false,
        power:"BOOK"
      }
    ]
  },

  formSubmit(res){
    let value=this.data.value
    // if(this.data.QXSelectList){
    //   let list=[]
    //   this.data.QXSelectList.forEach(item=>{
    //     list.push(item.power)
    //   })
    //   value.power=list.join()
    // }
    if(!value.position){
      wx.showToast({
        icon:"none",
        title: '请输入职位',
        duration:1500
      })
      return
    }else if(!value.nick){
      wx.showToast({
        icon:"none",
        title: '请输入名称',
        duration:1500
      })
      return
    }else if(!value.mobile){
      wx.showToast({
        icon:"none",
        title: '请输入手机号',
        duration:1500
      })
      return
    }else if(!value.context){
      wx.showToast({
        icon:"none",
        title: '请输入简介',
        duration:1500
      })
      return
    }
    if(this.data.staffDetail){
      value.shopAccountId=this.data.staffDetail.id
    }
    if(this.data.isEdit==false){
      controllers.saveStaff(this.data.value).then(res =>{
     
        if(res.code==1){
         wx.showToast({
           icon:'none',
           title: '添加成功',
           duration:2000
         })
   
         setTimeout(function(){
           wx.navigateBack({
             delta: 0,
           })
         },2000)
        }
       })
    }else{
      controllers.updateStaff(value).then(res=>{
        if(res.code==1){
          wx.showToast({
            icon:'none',
            title: '修改成功',
            duration:2000
          })
    
          setTimeout(function(){
            wx.navigateBack()
          },2000)
         }
      })
    }
  },

  //跳转添加权限
  toAddQX(){
    wx.navigateTo({
      url: '/pages/userStatistics/AddPersonnel/addQX?power='+this.data.value.power,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      'value.shopId':wx.getStorageSync('shopId')
    })
    if(options.staffDetail){
      this.setData({
        staffDetail:JSON.parse(decodeURIComponent(options.staffDetail)),
        isEdit:true
      })
      this.setData({
        "value.head":this.data.staffDetail.head||"",
        "value.position":this.data.staffDetail.position||"",
        "value.context":this.data.staffDetail.context||"",
        "value.nick":this.data.staffDetail.nick||"",
        "value.mobile":this.data.staffDetail.mobile||"",
        "value.level":this.data.staffDetail.level||"",
        "value.power":this.data.staffDetail.power||""
      })
      if(this.data.staffDetail.power){
        this.setData({
          selectIndex:0
        })
      }else{
        this.setData({
          selectIndex:1
        })
      }
    }

    let haveQXList=this.data.staffDetail.power.split(",")
    let QXList=this.data.QXList
    let list=[]
    QXList.forEach(item=>{
      for(let i=0;i<haveQXList.length;i++){
        if(item.power==haveQXList[i]){
          list.push(item)
        }
      }
    })
    this.setData({
      QXSelectList:list
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
   // onShareAppMessage: function () {

  // }

  //图片上传
  uploadImage(){
    let that=this
    wx.chooseImage({
      count: 1,
      success:(e)=>{
        let path=e.tempFilePaths[0];
        wx.uploadFile({
          filePath: path,
          name: 'image',
          url: 'https://www.xlshhr.com/beauty-industry/api/img/uploadFile',
          formData: {
            'Content-Type': 'application/form-data',
            'sessionId': wx.getStorageSync('token')
          },
          success (res){
           that.setData({
             'value.head':JSON.parse(res.data).data
           })
          }
        })
      }
    })
  },

  //获取员工姓名
  getStaffName(e){
    this.setData({
      'value.nick':e.detail.value
    })
  },

  //获取登录账号
  getlogonAccount(e){
    this.setData({
      'value.mobile':e.detail.value
    })
  },

   //获取员工职位
   getPosition(e){
    this.setData({
      'value.position':e.detail.value
    })
  },

  //获取简介
  getContent(e){
    this.setData({
      'value.context':e.detail.value
    })
  },
  //选择星级
  bindPickerChange(e){
    let index=e.detail.value
    this.setData({
      "value.level":this.data.levelList[index].id
    })
  },

  //选择权限
  selectPower(e){
    let selectIndex=e.currentTarget.dataset.index
    this.setData({
      selectIndex:selectIndex
    })
  }
})